//
//  HMNavgationViewController.swift
//  Weibo-swift
//
//  Created by GD on 16/9/19.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit

class HMNavgationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.interactivePopGestureRecognizer?.delegate = self;
        
//        let nav : UINavigationBar = UINavigationBar.init()
//        nav.backgroundColor = UIColor.orange
//        setValue(nav, forKey: "navBar")
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //  如果个数大于0表示不是根视图控制器
        if viewControllers.count > 0 {
            //  表示当前是第二个视图控制器
            if viewControllers.count == 1 {
                
                //  获取上一级控制的title
                let title = viewControllers.first!.title!
                viewController.hidesBottomBarWhenPushed = true
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
            } else {
                
                viewController.hidesBottomBarWhenPushed = true
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
            
            viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    func backAction()  {
        popViewController(animated: true)
    }

}

extension HMNavgationViewController:UIGestureRecognizerDelegate{
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1 {
            return false
        }
        return true
    }
}
